package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.undo.CannotUndoException;
import java.net.ConnectException;
import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    //查询购物车列表
    /*Request URL: http://localhost:8081/address/list
Request Method: GET*/
    // 查询购物车列表
    @GetMapping("/address/list")
    public ResultInfo addressList() {
        // 调用service查询
        List<Address> addresseList = addressService.addressList();
        // 返回结果
        return ResultInfo.success(addresseList);
    }

    //新增地址
  /*  Request URL: http://localhost:8081/address
    Request Method: POST*/
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address) {
        //接收参数
        //调用service保存
        addressService.save(address);
        //返回成功
        return ResultInfo.success(null);
    }
    //设置默认地址
    /*Request URL: http://localhost:8081/address/default
Request Method: PUT*/
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long>param) {//1.接收参数
        Long id = param.get("id");
        //2.调用service更新
        addressService.setDefault(id);
        //返回成功
        return ResultInfo.success(null);

    }

    //查询用户的m默认收货地址
    @GetMapping("/address/default")
    public ResultInfo findDefault() {
        //调用service查询
        Address address = addressService.findDefault();
        if (address == null) {
            throw new CustomException("请设置默认地址,结算购物车");
        }
        return ResultInfo.success(address);
    }

    //回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findAddressId(@PathVariable Long id) {//接收数据
        //调用service查询
         Address address=addressService.findAddressId(id);
        //返回结果
        return ResultInfo.success(address);

    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address) {
        //调用service查询
        addressService.updateAddress(address);
        //返回结果
        return ResultInfo.success(null);
    }

    //删除地址
    @DeleteMapping("/address")
    public ResultInfo deleteBatchId(@RequestParam List<Long>ids) {
        //调用service查询
        addressService.deleteBatchId(ids);
        //返回结果
        return ResultInfo.success(null);
    }



}
